//
//  editMyResumeViewController.swift
//  LeDian
//
//  Created by 三秒文化 on 16/3/18.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit
import YYModel
import BlocksKit
import Alamofire
import AFNetworking
import SwiftyJSON

//enum myResumeType:String {
//    case Voice = "语音微简历"
//    case Text = "文字微简历"
//}

class myResumeViewController: UIViewController {
    
    var resumeDict:Dictionary<String,AnyObject>? = nil {
        didSet{
            
            updateHeadView()
            tableView?.reloadData()
        }
    }
    var headview:myResumeHeadView?
    var BtnH:CGFloat = 60
    var tableView:UITableView?
    
    var type:MyResumeType?
    //cell数据,为统一返回的数据
    var model0 = myResumeCellNameModel()
    var model1 = myResumeCellMeritModel()
    var model2 = myResumeCellExperienceModel()
    var model3 = myResumeCellEduExperModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNav()
        setupTableView()
        setupBtn()
    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
//        
//        //初始化用户数据
//        _ = LDUserInfo.sharedInstance
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func sendRequest(){
        
        LDUserInfo.sharedInstance.queryJobFinderMyResume({ [weak self] (dict) in
            
            self?.resumeDict = dict
            //查询文字余音 微简历
            let titleStr = dict["isresumeaudio"] as? String
            
            if titleStr == "1" {
                
//                LDShowHUB.shareInstance.showBarStatus("当前为语音微简历")
                self?.type = .Voice
                self?.tableView?.reloadData()
            } else if titleStr == "0" {
                
//                LDShowHUB.shareInstance.showBarStatus("当前为文字微简历")
                self?.type = .Text
                self?.tableView?.reloadData()
            }
            
            
        }) { (error) in
        }
    }
    
    func updateHeadView() {
        let model = myResumeHeadViewModel.yy_modelWithDictionary(resumeDict!)
        headview?.model = model
    }
    
    func setupTableView() {
        
        view.backgroundColor = LDGolbalColor.White
        tableView = LDBaseTableView.init(frame: LDFrame.ScreenBounds, style: UITableViewStyle.Plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        view.addSubview(tableView!)
        
        tableView?.translatesAutoresizingMaskIntoConstraints = false
        tableView?.contentInset = UIEdgeInsetsMake(0, 0, BtnH + LDFrame.NavH, 0)
        headview = myResumeHeadView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, 80))
        tableView?.tableHeaderView = headview
    }
    
    override func viewWillDisappear(animated: Bool) {
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        tabBarController?.tabBar.hidden = true
        sendRequest()
//        tableView!.reloadData()
    }
    
    func setupNav() {
        
        self.title = "我的微简历"
        let bar = UIBarButtonItem.init(image:UIImage.init(named: "nav_share_white"), style: UIBarButtonItemStyle.Done, target: self, action: nil)
        self.navigationItem.rightBarButtonItem = bar
    }
    //底部编辑按钮
    func setupBtn() {
        
        let divideLine1 = UIView.init()
        divideLine1.backgroundColor = LDIconColor.LightGray
        
        let divideLine2 = UIView.init()
        divideLine2.backgroundColor = LDIconColor.LightGray
        
        let btn1 = UIButton.setDetailsBtn("管理期望工作", picName: "职位.png", btnBackColor: LDFontColor.White, titleColor: LDGolbalColor.Green)!
        btn1.titleLabel?.font = LDFont.Font18
        btn1.layer.cornerRadius = 0
        btn1.bk_whenTapped { [weak self] in
            self?.manageExpextJob()
        }
        
        let btn2 = UIButton.setDetailsBtn("编辑微简历", picName: "编辑信息.png", btnBackColor: LDFontColor.White, titleColor: LDGolbalColor.Green)!
        btn2.titleLabel?.font = LDFont.Font18
        btn2.layer.cornerRadius = 0
        btn2.bk_whenTapped { [weak self] in
            self?.editMyResume()
        }
        
        view.addSubview(btn1)
        view.addSubview(btn2)
        view.addSubview(divideLine1)
        view.addSubview(divideLine2)
        
        btn1.snp_makeConstraints { (make) in
            make.left.bottom.equalTo(0)
            make.right.equalTo(self.view.snp_centerX).offset(0)
            make.height.equalTo(BtnH)
        }
        
        btn2.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(0)
            make.left.equalTo(self.view.snp_centerX).offset(0)
            make.height.equalTo(BtnH)
        }
        divideLine1.snp_makeConstraints { (make) in
            make.top.equalTo(btn1)
            make.right.left.equalTo(0)
            make.height.equalTo(0.8)
        }
        divideLine2.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.centerY.equalTo(btn1)
            
            make.height.equalTo(BtnH - LDMargin_V*2)
            make.width.equalTo(0.8)
        }
    }
    //管理期望工作
    func manageExpextJob() {
        
        let vc = manageMyResumeVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    //编辑微简历
    func editMyResume() {
        
        let vc = editMyResumeVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func refreshInfo() {
        
    }
}
extension myResumeViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if type == .Text {
            
            return 4
            
        } else if type == .Voice {
            
            return 2
            
        } else {
            return 0
        }
        
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cells:UITableViewCell?
        if resumeDict == nil {
            return UITableViewCell.init()
        }
        
        switch indexPath.row {
            
        case 0:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cells1") as? myResumeCellName
            if cell == nil {
                cell = myResumeCellName.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls1")
            }
            cell!.models = model0
            return cell!
            
        case 1:
            
            if type == .Text {
                
                var cell = tableView.dequeueReusableCellWithIdentifier("cells2") as? myResumeCellMerit
                if cell == nil {
                    cell = myResumeCellMerit.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls2")
                }
                cell?.model = model1
                return cell!
                
            } else {
                
                let cell = myResumeCellVocie()
                return cell
            }
            
            
        case 2:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cells3") as? myResumeCellExperience
            if cell == nil {
                cell = myResumeCellExperience.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls3")
            }
            cell!.model = model2
            return cell!
            
        case 3:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cells3") as? myResumeCellEduExper
            if cell == nil {
                cell = myResumeCellEduExper.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellls3")
            }
            cell?.model = model3
            return cell!
            
        default:
            cells = UITableViewCell.init()
        }
        
        return cells!
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        var height:CGFloat = 0
        if resumeDict == nil {
            return 0
        }
        switch indexPath.row {
            
        case 0:
            
            model0 = myResumeCellNameModel.yy_modelWithDictionary(resumeDict!)!
            height = model0.height
            
        case 1:
            
            if type == .Voice {
                
                height = 330
                
            } else {
                model1 = myResumeCellMeritModel.yy_modelWithDictionary(resumeDict!)!
                height = model1.height
            }
            
            
        case 2:
            model2 = myResumeCellExperienceModel.yy_modelWithDictionary(resumeDict!)!
            height = model2.height
            
        case 3:
            model3 = myResumeCellEduExperModel.yy_modelWithDictionary(resumeDict!)!
            height = model3.height
            
        default:
            height = 0
        }
        return height
    }
}
